package leetcode.D600.T538;

import util.TreeNode;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-03-16 14:04:22 星期三
 */

class Solution {
    // 一刷
    /*public TreeNode convertBST(TreeNode root) {
        inorder(root, 0);
        return root;
    }

    private int inorder(TreeNode root, int sum) {
        if (root == null)
            return sum;
        sum = inorder(root.right, sum) + root.val;
        root.val = sum;
        return inorder(root.left, sum);
    }*/

    // 二刷
    private int sum = 0;
    public TreeNode convertBST(TreeNode root) {
        inorder(root);
        return root;
    }

    private void inorder(TreeNode root) {
        if (root == null)
            return;
        inorder(root.right);
        sum += root.val;
        root.val = sum;
        inorder(root.left);
    }
}